package serviceImpl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import bean.GoodsDetails;
import bean.TxtContent;
import service.TxtService;
import util.PropertyUtil;

/**
 * txt文本接口实现类
 * @author Administrator
 *
 */
public class TxtServiceImpl implements TxtService {

	/**
	 * 初始化txt文件列表
	 * @param txtPath  父路径
	 */
	@Override
	public List<String> showTxtList(String txtPath) {
		List<String> list = new ArrayList<String>();
		// 根据父路径遍历当前文件夹下所有txt文件
		File file = new File(txtPath);
		if(file.exists()){
			File[] files = file.listFiles();
			if (files.length == 0) {
                return list;
            } else {
                for (File file2 : files) {
                    if (!file2.isDirectory()&&file2.getAbsolutePath().endsWith("txt")) {
                    	list.add(file2.getName());
                    }
                }
            }
		}
		return list;
	}

	/**
	 * 双击读取txt文件内容
	 * @param file
	 * @return
	 */
	public TxtContent parseTxt(String txtName) {
		TxtContent content = new TxtContent();
		try {
			//获取txt文件路径
			content.setTxtName(txtName);
			String txtPath = PropertyUtil.getKeyValue("txtPath");
			File file = new File(txtPath+"\\"+txtName);
			if(file.exists()&&file.isFile()){
				InputStreamReader read = new InputStreamReader(new FileInputStream(file),"gbk");//考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				BigDecimal sumMon = new BigDecimal(0);
				BigDecimal sumJe = new BigDecimal(0);
				String lineTxt = null;
				int step = 0;
				
				List<GoodsDetails> list = new ArrayList<GoodsDetails>();
				//读第一行跳过
				bufferedReader.readLine();
				//读第二行
				lineTxt = bufferedReader.readLine();
				String[] str = lineTxt.split("~~");
				if(str.length>1){
					content.setDjh(str[0]);
					//content.setSph(str[1]);
					content.setGfmc(str[3]);
					//20170701版本使用
					content.setGfsh(str[10]);
					content.setGfdzdh(str[11]+" "+str[12]);
					content.setGfyhzh(str[13]+" "+str[14]);
					content.setBz(str[15]);
					
					//20170628版本使用
/*					content.setGfsh("");
					content.setGfdzdh("");
					content.setGfyhzh("");
					String bz = null;
					for (int i = 3; i < str.length; i++) {
						if(!str[i].equals("")){
							bz = str[i];
						}
					}
					content.setBz(bz);*/
					
					while((lineTxt = bufferedReader.readLine()) != null){
						step++;
						if(lineTxt.equals("")){
							break;
						}
						str = lineTxt.split("~~");
						GoodsDetails good = new GoodsDetails();
						good.setXh(String.valueOf(step));
						good.setSpCode(str[0]);
						good.setSpmc(str[1]);
						good.setJldw(str[2]);
						good.setGgxh(str[3]);
						good.setSl(str[4]);
						sumMon = sumMon.add(new BigDecimal(str[5]));
						good.setMon(str[5]);
						sumJe = sumJe.add(new BigDecimal(str[6]));
						good.setJe(str[6]);
						good.setSlv(str[7]);
						good.setSpbm(str[10]);
						list.add(good);
					}
					//添加合计一栏
					GoodsDetails sumGood = new GoodsDetails();
					sumGood.setSpmc("合计");
					sumGood.setJe(sumJe.toString());
					sumGood.setMon(sumMon.toString());
					list.add(sumGood);
					content.setSph(String.valueOf(step));
					content.setGoodsList(list);
					read.close();
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		    return content;
        }
	

}
